package com.dididoctor.doctor.Dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dididoctor.doctor.Activity.Usercentre.MyPurse.MyPurseView;
import com.dididoctor.doctor.MV.BaseDialog;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.MyToast;


public class CashDialog extends BaseDialog implements TextWatcher {
    private TextView mTvConfirm, mTvCancel, mTvTip;
    private EditText mEtInCome;
    private Context context;
    private double balance;
    private boolean isOk = true;

    public CashDialog(Context context, String num) {
        super(context);
        this.context = context;
        setContentView(R.layout.dialog_cash);
        mTvConfirm = (TextView) findViewById(R.id.tv_confirm);
        mTvConfirm.setOnClickListener(this);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mTvTip = (TextView) findViewById(R.id.tv_tip);
        mTvTip.setText("(账户余额:¥" + num + ")");
        mEtInCome = (EditText) findViewById(R.id.ed_incomebalance);
        mEtInCome.addTextChangedListener(this);
        balance = Double.parseDouble(num);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                if (checkValue()&&isOk) {
                    ((MyPurseView)context).withDrawals(mEtInCome.getText().toString());
                    dismiss();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
        super.onClick(v);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
       String text =  s.toString();
        if (text != null && !text.equals("")) {
            try {
                double curNum = Double.parseDouble(s.toString());
                if (curNum > balance  ) {
                    mTvTip.setText("输入金额超过账户余额");
                    mTvTip.setTextColor(Color.parseColor("#fc8713"));
                    isOk = false;
                } else{
                    mTvTip.setText("(账户余额:¥" + balance+")");
                    mTvTip.setTextColor(Color.parseColor("#888888"));
                    isOk = true;
                }
            }catch(Exception e) {
                MyToast.DefaultmakeText(context,"请正确输入提现金额！",0);
                mEtInCome.setText("");
            }


        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public boolean checkValue() {
        if (mEtInCome.getText().toString().trim().equals("")) {
            Toast.makeText(context, "请输入提现的金额", Toast.LENGTH_SHORT).show();
            return false;
        }

        if(Double.parseDouble(mEtInCome.getText().toString().trim())==0){
            Toast.makeText(context,"提现金额不能为0",Toast.LENGTH_LONG).show();
            mEtInCome.setText("");
            return false;
        }
        return true;
    }
}
